package com.fnzx.controller;

import com.fnzx.entity.ArticleEntity;
import com.fnzx.service.ArticleService;
import com.fnzx.util.PageUtils;
import com.fnzx.util.R;
import com.fnzx.util.oss.OssUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;


/**
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-10-07 10:54:12
 */
@RestController
@RequestMapping("iot/article")
@Api(tags = "文章模块")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * 列表
     */
    @ApiOperation(value = "后台页面-分页查询",notes = "查询出所有文章(page->当前页面,limit->每页记录数)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",paramType = "query",required = true,dataType = "String"),
            @ApiImplicitParam(name = "limit",value = "每页记录数",paramType = "query",required = true,dataType = "String")
    })
    @GetMapping("/list")
    public R list(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageUtils pg = articleService.queryPage(params);
        return R.ok().put("page", pg);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "后台页面-标题查询",notes = "通过文章标题查询出文章")
    @GetMapping("/info")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",paramType = "query",required = true,dataType = "String"),
            @ApiImplicitParam(name = "limit",value = "每页记录数",paramType = "query",required = true,dataType = "String"),
            @ApiImplicitParam(name = "title",value = "文章标题",paramType = "path",required = true,dataType = "String")
    })
    public R info(@ApiIgnore @RequestParam Map<String, Object> params){
        PageUtils page = articleService.queryPageByTitle(params);
        return R.ok().put("page", page);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "后台页面-添加", notes = "添加文章示例{\n" +
            "\t\"authorName\": \"1\",\n" +
            "\t\"coverImgUrl\": \"1\",\n" +
            "\t\"description\": \"1\",\n" +
            "\t\"linkUrl\": \"1\",\n" +
            "\t\"tags\": \"1\",\n" +
            "\t\"title\": \"1\",\n" +
            "\t\"type\": 1\n" +
            "}")
    @PostMapping("/save")
    public R save(@RequestBody ArticleEntity articleEntity, @RequestHeader String token){
        articleEntity.setViewNums(0);
        articleService.save(articleEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "后台页面-修改", notes = "修改文章{\n" +
            "\t\"authorName\": \"\",\n" +
            "\t\"coverImgUrl\": \"\",\n" +
            "\t\"description\": \"\",\n" +
            "\t\"id\": 0,\n" +
            "\t\"linkUrl\": \"\",\n" +
            "\t\"tags\": \"\",\n" +
            "\t\"title\": \"\",\n" +
            "\t\"type\": 0,\n" +
            "}")
    @PostMapping("/update")
    public R update(@RequestBody ArticleEntity articleEntity, @RequestHeader String token){
        Long id = articleEntity.getId();
        String coverImgUrl = articleService.getById(id).getCoverImgUrl();
        if (!articleEntity.getCoverImgUrl().equals(coverImgUrl)) {
            OssUtils.delete(coverImgUrl);
        }
        articleService.updateById(articleEntity);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "后台页面-删除", notes = "删除文章，可以同时删除多个{1,2}")
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids,@RequestHeader String token){
        if (ids.length != 0 && ids != null) {
            for (Long id : ids) {
                ArticleEntity articleEntity = articleService.getById(id);
                String url = articleEntity.getCoverImgUrl();
                OssUtils.delete(url);
            }
            articleService.removeByIds(Arrays.asList(ids));
            return R.ok();
        }
        return R.ok("id不能为空");
    }

    @ApiOperation(value = "上传文件", notes = "上传文件（图片和视频）到阿里云oss")
    @PostMapping("fileUpload")
    public R fileUpload(MultipartFile file,@RequestHeader String token) throws IOException {
        //获取上传文件
        String url = OssUtils.upload(file);
        return R.ok(url);
    }

    /**
     * 前台显示
     */
    @ApiOperation(value = "前台和后台-根据文章类型进行分页查询",notes = "根据类型查询文章(page->当前页面,limit->每页记录数,type->文章类型(1->资讯新闻，2->专家文章，3->领导关怀，4->客户案例))")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",paramType = "query",required = true,dataType = "String"),
            @ApiImplicitParam(name = "limit",value = "每页记录数",paramType = "query",required = true,dataType = "String"),
            @ApiImplicitParam(name = "type",value = "文章类型",paramType = "query",required = false,dataType = "String")
    })
    @GetMapping("/list/")
    public R listByType(@ApiIgnore @RequestParam Map<String, Object> params){
        PageUtils pg = articleService.queryPageByType(params);
        return R.ok().put("page", pg);
    }

    /**
     * 前台显示
     */
    @ApiOperation(value = "前台页面-搜索分页接口",notes = "根据类型查询文章(page->当前页面,limit->每页记录数,type->文章类型(1->行情咨询，2->专家文章，3->领导关怀，4->质询文章),search（标题，作者，描述）)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",paramType = "query",required = true,dataType = "String"),
            @ApiImplicitParam(name = "limit",value = "每页记录数",paramType = "query",required = true,dataType = "String"),
            @ApiImplicitParam(name = "type",value = "文章类型()",paramType = "query",required = false,dataType = "String"),
            @ApiImplicitParam(name = "search",value = "搜索（标题，作者，描述）",paramType = "query",required = false,dataType = "String"),
    })
    @GetMapping("info/")
    public R listBy(@ApiIgnore @RequestParam Map<String, Object> params){
        PageUtils pg = articleService.queryPageBy(params);
        return R.ok().put("page", pg);
    }

    @ApiOperation(value = "删除文件",notes = "删除文件，url -> 文件地址")
    @DeleteMapping("/delete")
    public R deleteFile(@RequestParam("url") String url, @RequestHeader String token) {
        OssUtils.delete(url);
        return R.ok();
    }


}
